#include "plzwait.h"

argstruct *
argc_argv_parse(int argc, char **argv) {

	//. this function requires the typedef struct _argc_argv, which has been
	//. placed in plzwait.h for this application.

	int a = 0;
	argstruct *arg = (argstruct *)malloc(sizeof(argstruct));
	
	arg->version = 0;
	arg->help = 0;
	arg->unknown = 0;	
	arg->command = NULL;
	arg->message = NULL;
	arg->time = 0;
	arg->show_window_border = 0;
	arg->width = WIDTH;
	arg->height = HEIGHT;
	arg->padding = PADDING;
	
	while(a < argc) {

		if(!strcmp("--version",argv[a])) {
			arg->version = 1;
		}
		
		else if(!strcmp("--help",argv[a])) {
			arg->help = 1;
		}

		else if(!strcmp("--command",argv[a])) {
			int cmdlen = strlen(argv[++a]) + strlen(CMD_SUFFIX);
			arg->command = (char *)malloc((sizeof(char) * cmdlen));
			strncpy(arg->command,argv[a],(sizeof(char) * cmdlen));
			strcat(arg->command,CMD_SUFFIX);
		}

		else if(!strcmp("--message",argv[a])) {
			int msglen = strlen(argv[++a]);
			arg->message = (char *)malloc((sizeof(char) * msglen));
			strncpy(arg->message,argv[a],(sizeof(char) * msglen));
		}

		else if(!strcmp("--time",argv[a])) {
			arg->time = (unsigned int)ceil(
				((double)strtod(argv[++a],NULL) * (double)1000)
			);
		}

		else if(!strcmp("--border",argv[a])) {
			arg->show_window_border = 1;
		}

		else if(!strcmp("--width",argv[a])) {
			arg->width = atoi(argv[++a]);
		}

		else if(!strcmp("--height",argv[a])) {
			arg->height = atoi(argv[++a]);
		}

		else if(!strcmp("--padding",argv[a])) {
			arg->padding = atoi(argv[++a]);
		}
		
		else if(a > 0) {
			arg->help = 1;
			arg->unknown = 1;	
		}
		
		++a;
	}	

	return arg;
}
